package com.example.service.impl;

import com.example.mapper.CarMapper;
import com.example.pojo.Car;
import com.example.pojo.CarPageQueryDTO;
import com.example.pojo.PageResult;
import com.example.service.CarService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CarServiceImpl implements CarService {
@Autowired
private CarMapper carMapper;


    public void add(Car car) {
      carMapper.insert(car);
    }



    public void getCarById(Integer id) {

         carMapper.delete(id);
    }


    public void lendCar(Integer id,Integer status) {
//findStatus
       Car car1=carMapper.findStatus(id);
     //默认0为借出，1为未借出
        int status1 = car1.getStatus();
        if (status1==1){
        carMapper.lendCar(id);
     }else {
         throw  new RuntimeException("该车辆已被借出");
     }

    }


    public PageResult pageQuery(CarPageQueryDTO carPageQueryDTO) {

        Integer num= carPageQueryDTO.getPage();
        if (num!=null){
            int value=num.intValue();
            PageHelper.startPage(value,carPageQueryDTO.getPageSize());
        }
        Page<Car> page=carMapper.pageQuery(carPageQueryDTO);
        long total = page.getTotal();
        List<Car> records = page.getResult();
        return new PageResult(total,records,1,0,true,true,null,null,true,0,null);

    }


    public void revert(Integer id, Integer status) {
        carMapper.revert(id);
    }


}
